//
//  XRAFNetworkTool.m
//  框架TEST1
//
//  Created by dllo on 15/12/9.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "XRAFNetworkTool.h"
#import <AFNetworking/AFNetworking.h>
@implementation XRAFNetworkTool

+ (void)GETUrl:(NSString *)url
          body:(id)body
      headFile:(NSDictionary *)header
      response:(XRResponseStyle)responseStyle
       success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
       failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
{
    // 1. 获取AFN网络请求的Manager
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 2. 请求头
    if (header) {
        for (NSString *key in header.allKeys) {
            [manager.requestSerializer setValue:header[key] forHTTPHeaderField:key];
        }
    }
    
    // 3. 数据返回类型的设定 JSON . XML . DATA
    switch (responseStyle) {
        case XRJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case XRXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        case XRDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        default:
            break;
    }
    
    // 4. 设定响应数据类型。
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
    
    // 5. UTF8
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    // 6. 请求
    [manager GET:url parameters:body success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        if (responseObject) {
            success(task, responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
        if (error) {
            failure(task, error);
        }
        
    }];
}

+ (void)POSTUrl:(NSString *)url
           body:(id)body
requestBodyStyle:(XRRequestStyle)bodyStyle
       headFile:(NSDictionary *)header
       response:(XRResponseStyle)responseStyle
        success:(void (^)(NSURLSessionDataTask *, id))success
        failure:(void (^)(NSURLSessionDataTask *, NSError *))failure
{
    // 1. 获取AFN网络请求的Manager
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 2. 设置body的类型
    switch (bodyStyle) {
            
        case XRRequestJSON:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
        case XRRequestString:
            
            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable __autoreleasing * _Nullable error) {
                return parameters;
            }];
             break;
        default:
            break;
    }

    // 3. 请求头 ---- 通过请求头告诉服务器，客户端的类型
    if (header) {
        for (NSString *key in header.allKeys) {
            [manager.requestSerializer setValue:header[key] forHTTPHeaderField:key];
        }
    }
    
    // 4. 数据返回类型的设定 JSON . XML . DATA
    switch (responseStyle) {
        case XRJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case XRXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        case XRDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        default:
            break;
    }
    
    // 5. 设定响应数据类型。
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"application/x-javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
    
    // 6. UTF8
     url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    // 7. 请求
   [manager POST:url parameters:body success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
       if (responseObject) {
           NSString *string = responseObject;
           success(task, string);
       }
   } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
       if (error) {
           failure(task, error);
       }
   }];
    
}
@end
